package me.yushuai.wukong.base.repository;

import me.yushuai.wukong.base.entity.Location;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
 * 针对表【BAS_LOCATION】的数据库操作Mapper
 *
 * @author zhoushuai@189.cn
 * @since 2022-08-31 09:32:32
 */
@Repository
public interface LocationRepository extends JpaRepository<Location, Long>, JpaSpecificationExecutor<Location> {

    @Query("from Location  where  id = ?1 and bizId = ?2")
    Optional<Location> findLocByIdAndBizId(Long id, String bizId);

    /**
     * 检查库位是否在指定仓库
     *
     * @param warehouseId 仓库Id
     * @param locId       库位Id
     * @return 返回true表示仓库下存在库位Id否则表示不存在
     */
    @Query("from Location l, LocGroup g where (l.locGroup1Id = g.groupId or l.locGroup2Id = g.groupId) " +
            "and l.id = ?2 and g.warehouseId = ?1")
    boolean existsByWarehouseIdAndLocId(Long warehouseId, Long locId);

    /**
     * 使用库位的业务Id查询库位的详细信息
     *
     * @param bizId 库位的业务Id
     * @return 返回库位的详细信息Optional对象
     */
    @Query("from Location where bizId = ?1")
    Optional<Location> findByBizId(String bizId);
}




